/*
 * System.hpp
 *
 *  Created on: Jan 22, 2015
 *      Author: wasp
 */

#ifndef SYSTEM_HPP_
#define SYSTEM_HPP_

#include <huctx.h>
#include <qfile.h>
#include <qstring.h>
#include <openssl/rsa.h>

namespace de
{
    namespace heimweghelden
    {
        namespace security
        {
            class Session;
            class System
            {
            public:
                static System* getInstance();
                static void drop();
                static System* _instance;
                void deleteRSA();
                Session* createSession();
            private:
                System();
                virtual ~System();
                void log(int code, QString info);
                bool init();
                bool initRSA();
                bool keypairExists();
                bool createKeypair();
                int createRSAFromKeypair();
                bool saveRSA();
                bool writeRSAPem(bool pkey);
                bool writeFile(QString data, QFile* file);
                void printRSA(RSA* keystore);
                QString RSAToString(RSA* keystore, bool prikey);
                bool loadKeypair();
                void publishPublicKey();

                void test(QString value);


                QString myPrivateKeyFilename;
                QString myPublicKeyFilename;
                sb_GlobalCtx sbCtx;
                sb_RNGCtx rngCtx;
                sb_Params rsaParams;
                sb_PrivateKey myPrivateKey;
                sb_PublicKey myPublicKey;
                RSA* myKeypair;
            };

        } /* namespace security */
    } /* namespace heimweghelden */
} /* namespace de */

#endif /* SYSTEM_HPP_ */
